package webjoch.process.strategies.puzzles;

import java.awt.Graphics2D;

import org.rsbuddy.tabs.Inventory;

import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.task.Task;

import webjoch.misc.Constants;
import webjoch.process.strategies.Puzzle;
import webjoch.wrappers.Room;
/**
 * 
 * @author Webjoch
 *
 */
public class WinchBridge extends Puzzle{
	Room room;
	
	public final int GRAPPLE = 39912;
	public final int GRAPPLE_WITH_ROPE = 39914;
	public final int CRATE_MEATCORN = 54568;
	public final int[] CRATE_HOOK = {54645, 54567}; //Expirimental change, got nullpointers with old ID
	public final int ANVIL = 54257;
	public final int WHEEL = 49934;
	public final int WINCH = 39917;
	
	public final int BROKEN_HOOK_HEAD = 19663;
	public final int MEATCORN = 19665;
	public final int HOOK_HEAD = 19664;
	public final int ROPE = 19667;
	public final int CLIMBING_HOOK = 19668;
	
	
	@Override
	public void setRoom(Room room) {
		this.room = room;
	}

	@Override
	public boolean isValid() {
		return room.getObject(GRAPPLE) != null || room.getObject(GRAPPLE_WITH_ROPE) != null;
	}

	@Override
	public Type getType() {
		return Type.WINCH_BRIDGE;
	}

	@Override
	public int loop() {
		if (room.getObject(GRAPPLE_WITH_ROPE) != null){
			room.getObject(WINCH).interact("Operate");
			Task.sleep(1000);
			return 1;
		}
		if (!Inventory.contains(BROKEN_HOOK_HEAD) && !Inventory.contains(HOOK_HEAD) && !Inventory.contains(CLIMBING_HOOK)){
			room.getObject(CRATE_HOOK).interact("Take-from");
			waitForItem(BROKEN_HOOK_HEAD);
			return 1;
		}
		if (!Inventory.contains(MEATCORN) && !Inventory.contains(ROPE) && !Inventory.contains(CLIMBING_HOOK)){
			room.getObject(CRATE_MEATCORN).interact("Take-from");
			waitForItem(MEATCORN);
			return 1;
		}
		if (Inventory.contains(BROKEN_HOOK_HEAD)){
			room.getObject(ANVIL).interact("Smith");
			waitForItem(HOOK_HEAD);
			return 1;
		}
		if (Inventory.contains(MEATCORN)){
			room.getObject(WHEEL).interact("Spin");
			waitForItem(ROPE);
			return 1;
		}
		if (Inventory.contains(HOOK_HEAD) && Inventory.contains(ROPE)){
			Inventory.useItem(Inventory.getItem(HOOK_HEAD), Inventory.getItem(ROPE));
			waitForItem(CLIMBING_HOOK);
			return 1;
		}
		if (Inventory.contains(CLIMBING_HOOK)){
			Inventory.getItem(CLIMBING_HOOK).interact("Use"); Task.sleep(500, 800);
			if (Inventory.isItemSelected()){
				room.getObject(GRAPPLE).interact("Use");
				
				for (int i = 0; i < 100; i++){
					if (room.getObject(GRAPPLE_WITH_ROPE) != null) break;
					Task.sleep(50);
				}
				return 1;
			}
		}
		return 0;
	}

	@Override
	public int[] itemNeed() {
		return new int[] {Constants.HAMMER[0]};
	}

	@Override
	public boolean isFinished() {
		return false;
	}

	@Override
	public void messageReceived(MessageEvent e) {
		
	}
	
	public void waitForItem(int itemId){
		for (int i = 0; i < 100; i++){
			if (Inventory.contains(itemId)) break;
			Task.sleep(30);
		}
	}

	@Override
	public void onRepaint(Graphics2D g) {
		// TODO Auto-generated method stub
		
	}
}
